#!/sbin/openrc-run

XE_LINUX_DISTRIBUTION=/usr/sbin/xe-linux-distribution
XE_LINUX_DISTRIBUTION_CACHE=/var/cache/xe-linux-distribution
XE_DAEMON=/usr/sbin/xe-daemon
XE_DAEMON_PIDFILE=/var/run/xe-daemon.pid

name=xe-daemon
daemon=/usr/sbin/$name

depend() {
	need localmount
	after bootmisc
}

start() {
	if [ ! -d /proc/xen ]; then
		return
	fi

	ebegin "Detecting Linux distribution version"
		${XE_LINUX_DISTRIBUTION} ${XE_LINUX_DISTRIBUTION_CACHE}
	eend $?

	if [ ! -e /proc/xen/xenbus ] ; then
		ebegin "Mounting xenfs on /proc/xen"
			if [ ! -d /proc/xen ] ; then
				/bin/false
			else
				mount -t xenfs none /proc/xen
			fi
		eend $?
	fi

	ebegin "Starting ${name}"
		mkdir -p $(dirname ${XE_DAEMON_PIDFILE})
		( exec &>/dev/null ; ${XE_DAEMON} -p ${XE_DAEMON_PIDFILE} & )
	eend $?
}

stop() {
	if [ ! -d /proc/xen ]; then
		return
	fi

	ebegin "Stopping ${name}"
		kill -TERM $(cat ${XE_DAEMON_PIDFILE})
	eend $?
}

